#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
添加标签对话框
"""

from PyQt5.QtWidgets import (
    QDialog, QVBoxLayout, QHBoxLayout, QFormLayout,
    QLabel, QLineEdit, QPushButton, QColorDialog,
    QMessageBox
)
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QColor

class ColorPreview(QLabel):
    """颜色预览控件"""
    
    def __init__(self, color=Qt.red, parent=None):
        """初始化"""
        super().__init__(parent)
        self.color = color
        self.setFixedSize(24, 24)
        self.setStyleSheet(f"background-color: {color.name()}; border: 1px solid black;")
        
    def setColor(self, color):
        """设置颜色"""
        self.color = color
        self.setStyleSheet(f"background-color: {color.name()}; border: 1px solid black;")


class AddLabelDialog(QDialog):
    """添加标签对话框"""
    
    # 自定义信号
    label_added = pyqtSignal(str, QColor)  # 标签添加信号，传递标签名称和颜色
    
    def __init__(self, parent=None):
        """初始化"""
        super().__init__(parent)
        
        # 设置对话框属性
        self.setWindowTitle("添加标签")
        self.setMinimumWidth(300)
        
        # 初始化颜色
        self.current_color = QColor(255, 0, 0)  # 默认红色
        
        # 初始化UI
        self._setup_ui()
    
    def _setup_ui(self):
        """设置界面"""
        # 主布局
        main_layout = QVBoxLayout(self)
        
        # 表单布局
        form_layout = QFormLayout()
        
        # 标签名称输入
        self.label_name = QLineEdit()
        self.label_name.setPlaceholderText("输入标签名称")
        form_layout.addRow("标签名称:", self.label_name)
        
        # 颜色选择
        color_layout = QHBoxLayout()
        self.color_preview = ColorPreview(self.current_color)
        self.color_button = QPushButton("选择颜色")
        self.color_button.clicked.connect(self._on_color_button_clicked)
        
        color_layout.addWidget(self.color_preview)
        color_layout.addWidget(self.color_button)
        color_layout.addStretch()
        
        form_layout.addRow("标签颜色:", color_layout)
        
        main_layout.addLayout(form_layout)
        
        # 按钮布局
        button_layout = QHBoxLayout()
        self.cancel_button = QPushButton("取消")
        self.cancel_button.clicked.connect(self.reject)
        self.add_button = QPushButton("添加")
        self.add_button.clicked.connect(self._on_add_button_clicked)
        self.add_button.setDefault(True)
        
        button_layout.addWidget(self.cancel_button)
        button_layout.addWidget(self.add_button)
        
        main_layout.addLayout(button_layout)
    
    def _on_color_button_clicked(self):
        """颜色按钮点击事件"""
        color = QColorDialog.getColor(self.current_color, self, "选择标签颜色")
        if color.isValid():
            self.current_color = color
            self.color_preview.setColor(color)
    
    def _on_add_button_clicked(self):
        """添加按钮点击事件"""
        # 获取标签名称
        label_name = self.label_name.text().strip()
        
        # 验证名称
        if not label_name:
            QMessageBox.warning(self, "验证错误", "请输入标签名称")
            self.label_name.setFocus()
            return
        
        # 发射信号
        self.label_added.emit(label_name, self.current_color)
        
        # 关闭对话框
        self.accept()
    
    def get_result(self):
        """
        获取对话框结果
        
        Returns:
            tuple: (标签名称, 颜色)
        """
        return (self.label_name.text().strip(), self.current_color) 